import type { FC } from 'react';
import { Outlet, useLocation, Navigate } from "react-router-dom";
import { useToken } from '@/hooks/useSession';

const mainPath = '/';

export const AppAuth: FC = () => {
  const { pathname } = useLocation();
  const [token, loading] = useToken();

  if (loading) {
    return null;
  }

  if (token) {
    if (pathname != mainPath) {
      return <Navigate to="/" replace />;
    }
  } else {
    if (pathname == mainPath) {
      return <Navigate to="/signin" replace />;
    }
  }
  return <Outlet key={pathname} />;
}

export default AppAuth;